/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.ledger.account.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.redragon.commons.lang.JavaReflect;
import org.redragon.commons.lang.StringUtil;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;
import org.redragon.framework.util.ShiroUtil;

import com.erp.ledger.account.dao.model.CoaSegment;
import com.erp.ledger.account.dao.model.GLCodeCombinations;
import com.erp.ledger.account.dao.model.GLCodeCombinationsCO;
import com.erp.ledger.account.dao.model.GLLedger;
import com.erp.ledger.account.service.GLCodeCombinationsService;
import com.erp.ledger.account.service.GLLedgerService;
import com.erp.masterdata.common.service.MasterDataCommonService;

@Controller
@RequestMapping("/web/gLCodeCombinations")
public class GLCodeCombinationsWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(GLCodeCombinationsWebController.class);
    
    //服务层注入
    @Autowired
    private GLCodeCombinationsService gLCodeCombinationsService;
    @Autowired
    private GLLedgerService glLedgerService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    
    @Override
    public String getExceptionRedirectUrl() {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-01-26 16:16:03
     * @author 
     * @param pages
     * @param gLCodeCombinationsCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLCodeCombinationsList")
    public String getGLCodeCombinationsList(Pages pages, GLCodeCombinationsCO gLCodeCombinationsCO, Model model) {
        //TODO: 分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(10);
        pages.setShowPageNum(7);
        pages.setStartEndPageNum(2);
        
        //分页查询数据
        List<GLCodeCombinations> gLCodeCombinationsList = this.gLCodeCombinationsService.getDataObjects(pages, gLCodeCombinationsCO);
        
        //页面属性设置
        model.addAttribute("gLCodeCombinationsList", gLCodeCombinationsList);
        model.addAttribute("pages", pages);
        
        return "gLCodeCombinations/gLCodeCombinationsList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-01-26 16:16:03
     * @author 
     * @param gLCodeCombinations
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLCodeCombinations")
    public String getGLCodeCombinations(GLCodeCombinations gLCodeCombinations, Model model) {
        //TODO: 查询要编辑的数据
        
        //页面属性设置
        model.addAttribute("gLCodeCombinations", gLCodeCombinations);
        
        return "gLCodeCombinations/gLCodeCombinationsEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-01-26 16:16:03
     * @author 
     * @param gLCodeCombinations
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editGLCodeCombinations")
    public String editGLCodeCombinations(@Valid GLCodeCombinations gLCodeCombinations, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getGLCodeCombinations";
        }
        
        //TODO: 对当前编辑的对象初始化默认的字段
        
        //保存编辑的数据
        this.gLCodeCombinationsService.insertOrUpdateDataObject(gLCodeCombinations);
        //提示信息
        attr.addFlashAttribute("hint", "success");
        
        return "redirect:getGLCodeCombinationsList";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-01-26 16:16:03
     * @author 
     * @param gLCodeCombinations
     * @return String
     *
     */
    @RequestMapping("deleteGLCodeCombinations")
    public String deleteGLCodeCombinations(GLCodeCombinations gLCodeCombinations, RedirectAttributes attr) {
        //TODO: 删除数据前验证数据
        
        //删除数据
        this.gLCodeCombinationsService.deleteDataObject(gLCodeCombinations);
        //提示信息
        attr.addFlashAttribute("hint", "success");
        
        return "redirect:getGLCodeCombinationsList";
    }
    
    
    
    /**
     * 
     * @description 初始化会计科目结构弹层
     * @date 2021年1月27日
     * @author dongbin
     * @param model
     * @return
     * @return String
     *
     */
    @RequestMapping("initCOA")
    public String initCOA(String subjectCode, Model model) {
        //获取法人的主分类账
        GLLedger glLedger = this.glLedgerService.getPrimaryGLLedgerByLegal(ShiroUtil.getLegalCode());
        
        //反射获取segment值
        JavaReflect reflectLedger = new JavaReflect(glLedger.getClass());
        reflectLedger.setObject(glLedger);
        
        GLCodeCombinations glCodeCombinations = new GLCodeCombinations();
        //获取会计科目结构值
        if(StringUtils.isNotBlank(subjectCode)) {
            glCodeCombinations = this.gLCodeCombinationsService.getDataObject(subjectCode);
            if(glCodeCombinations==null) {
                glCodeCombinations = new GLCodeCombinations();
            }
        }
        
        //反射获取科目结构值
        JavaReflect reflect = new JavaReflect(glCodeCombinations.getClass());
        reflect.setObject(glCodeCombinations);
        
        //段值List
        List<CoaSegment> coa = new ArrayList<CoaSegment>();
        
        for(int a=1;a<=15;a++) {
            String segmentTypeTemp = (String)reflectLedger.doMethod("getSegment"+a, null, null);
            String segmentTemp = (String)reflect.doMethod("getSegment"+a, null, null);
            
            if(StringUtils.isNotBlank(segmentTypeTemp)) {
                CoaSegment coaSegment = new CoaSegment();
                coaSegment.setSegmentField("segment"+a);
                coaSegment.setSegmentFieldType(segmentTypeTemp);
                
                if(segmentTypeTemp.contentEquals("SEGMENT_COMPANY")) {
                    coaSegment.setSegmentFieldName("公司");
                    coaSegment.setSegmentDataType("URL");
                    coaSegment.setSegmentFieldValue(segmentTemp);
                    coaSegment.setSegmentFieldValueDesc(this.gLCodeCombinationsService.getGLCodeCombinationsSingleSegmentDesc(segmentTypeTemp, segmentTemp));
                }else if(segmentTypeTemp.contentEquals("SEGMENT_COST_CENTER")) {
                    coaSegment.setSegmentFieldName("成本中心");
                    coaSegment.setSegmentDataType("URL");
                    coaSegment.setSegmentFieldValue(segmentTemp);
                    coaSegment.setSegmentFieldValueDesc(this.gLCodeCombinationsService.getGLCodeCombinationsSingleSegmentDesc(segmentTypeTemp, segmentTemp));
                }else if(segmentTypeTemp.contentEquals("SEGMENT_SUBJECT")) {
                    coaSegment.setSegmentFieldName("科目");
                    coaSegment.setSegmentDataType("URL");
                    coaSegment.setSegmentFieldValue(segmentTemp);
                    coaSegment.setSegmentFieldValueDesc(this.gLCodeCombinationsService.getGLCodeCombinationsSingleSegmentDesc(segmentTypeTemp, segmentTemp));
                }else if(segmentTypeTemp.contentEquals("SEGMENT_DETAIL_SUBJECT")) {
                    coaSegment.setSegmentFieldName("明细科目");
                    coaSegment.setSegmentDataType("URL");
                    coaSegment.setSegmentFieldValue(segmentTemp);
                    coaSegment.setSegmentFieldValueDesc(this.gLCodeCombinationsService.getGLCodeCombinationsSingleSegmentDesc(segmentTypeTemp, segmentTemp));
                }else if(segmentTypeTemp.contentEquals("SEGMENT_PROJECT")) {
                    coaSegment.setSegmentFieldName("项目");
                    coaSegment.setSegmentDataType("MAP");
                    coaSegment.setSegmentDataMap(this.masterDataCommonService.getProjectMap());
                    coaSegment.setSegmentFieldValue(segmentTemp);
                }else if(segmentTypeTemp.contentEquals("SEGMENT_PRODUCT")) {
                    coaSegment.setSegmentFieldName("产品");
                    coaSegment.setSegmentDataType("MAP");
                    coaSegment.setSegmentDataMap(this.masterDataCommonService.getMaterialMap());
                    coaSegment.setSegmentFieldValue(segmentTemp);
                }else if(segmentTypeTemp.contentEquals("SEGMENT_CUSTOMER")) {
                    coaSegment.setSegmentFieldName("客户");
                    coaSegment.setSegmentDataType("MAP");
                    coaSegment.setSegmentDataMap(this.masterDataCommonService.getCustomerMap());
                    coaSegment.setSegmentFieldValue(segmentTemp);
                }else if(segmentTypeTemp.contentEquals("SEGMENT_VENDOR")) {
                    coaSegment.setSegmentFieldName("供应商");
                    coaSegment.setSegmentDataType("MAP");
                    coaSegment.setSegmentDataMap(this.masterDataCommonService.getVendorMap());
                    coaSegment.setSegmentFieldValue(segmentTemp);
                }else if(segmentTypeTemp.contentEquals("SEGMENT_INTER_COMPANY")) {
                    coaSegment.setSegmentFieldName("往来");
                    coaSegment.setSegmentDataType("URL");
                    coaSegment.setSegmentFieldValue(segmentTemp);
                    coaSegment.setSegmentFieldValueDesc(this.gLCodeCombinationsService.getGLCodeCombinationsSingleSegmentDesc(segmentTypeTemp, segmentTemp));
                }
                
                coa.add(coaSegment);
            }else {
                break;
            }
        }
        
        //页面属性设置
        model.addAttribute("coa", coa);
        
        return "gLCodeCombinations/pop/coaModal";
    }
    
    @RequestMapping("subjectDetailTreeModal")
    public String subjectDetailTreeModal(String id, Model model) {
        return "gLCodeCombinations/pop/subjectDetailTreeModal";
    }
    
    @RequestMapping("subjectTreeModal")
    public String subjectTreeModal(String id, Model model) {
        return "gLCodeCombinations/pop/subjectTreeModal";
    }
    
    @RequestMapping("companyTreeModal")
    public String companyTreeModal(String id, Model model) {
        return "gLCodeCombinations/pop/companyTreeModal";
    }
    
    @RequestMapping("costCenterTreeModal")
    public String costCenterTreeModal(String id, Model model) {
        return "gLCodeCombinations/pop/costCenterTreeModal";
    }
    
    @RequestMapping("getOrInsertGLCodeCombinations")
    @ResponseBody
    public String getOrInsertGLCodeCombinations(GLCodeCombinations glCodeCombinations, Model model) {
        String codeCombinationCode = this.gLCodeCombinationsService.getOrInsertGLCodeCombinations(glCodeCombinations);
        String segment = this.gLCodeCombinationsService.getGLCodeCombinationsSegment(codeCombinationCode);
        String segmentDesc = this.gLCodeCombinationsService.getGLCodeCombinationsSegmentDesc(codeCombinationCode);
        
        return "{\"codeCombinationCode\":\""+codeCombinationCode+"\", \"segment\":\""+segment+"\", \"segmentDesc\":\""+segmentDesc+"\"}";
    }
}
